<?php  namespace Common\Model;

 use Think\Model;
//继承BaseModel执行它里面的自动验证方法
 class CodeModel extends BaseModel{
     //自动验证方法
     protected $pk = 'id';
     protected $tableName = 'user';
     protected $_validate =[
       ['username','require','用户名不能为空'],
       ['password','require','密码不能为空'],
       ['code','require','验证码不能为空'],
     ];

     /**账号密码登陆方法【应用在控制器里面】
      * @return array
      */
    public function login()
    {
        //用户名密码进行比对
        $username = $_POST['username'];
        $password =md5($_POST ['password']);
        $where=[
            'username'=>$username,
            'password'=>$password,
        ];
        $data = m('user')->where($where)->find();
        if(is_null($data)){
            return ['valid'=>false,'msg'=>'用户名密码不能为空'];
        }
        session('uid',$data['id']);
        session('username',$data['username']);
        return  ['valid'=>true,'msg'=>'登陆成功'];exit;
    }





}
